<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="需求计划编号" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="需求计划编号" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="物资编码" prop="materialNum">
      <el-input v-model="dataForm.materialNum" placeholder="物资编码" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="物资名称" prop="materialName">
      <el-input v-model="dataForm.materialName" placeholder="物资名称" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="amount">
      <el-input v-model="dataForm.amount" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="计量单位" prop="measureUnit">
      <el-input v-model="dataForm.measureUnit" placeholder="计量单位" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="预算单价(元)" prop="unitPrice">
      <el-input v-model="dataForm.unitPrice" placeholder="预算单价(元)"></el-input>
    </el-form-item>
    <el-form-item label="编制人" prop="author">
      <el-input v-model="dataForm.author" placeholder="编制人" :disabled="true"></el-input>
    </el-form-item>
    <el-form-item label="联系电话" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
    </el-form-item>
    <el-form-item label="联系传真" prop="fax">
      <el-input v-model="dataForm.fax" placeholder="联系传真"></el-input>
    </el-form-item>
    <el-form-item label="电子邮件" prop="email">
      <el-input v-model="dataForm.email" placeholder="电子邮件"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderNum: '',
          orderSource: '',
          materialNum: '',
          materialName: '',
          amount: '',
          measureUnit: '',
          unitPrice: '',
          sumPrice: '',
          startDate: '',
          endDate: '',
          address: '',
          authorId: '',
          author: '',
          phone: '',
          fax: '',
          email: '',
          remark: ''
        },
        dataRule: {
          orderNum: [
            { required: true, message: '需求计划编号不能为空', trigger: 'blur' }
          ],
          orderSource: [
            { required: true, message: '需求来源不能为空', trigger: 'blur' }
          ],
          materialNum: [
            { required: true, message: '物资编码不能为空', trigger: 'blur' }
          ],
          materialName: [
            { required: true, message: '物资名称不能为空', trigger: 'blur' }
          ],
          amount: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          measureUnit: [
            { required: true, message: '计量单位不能为空', trigger: 'blur' }
          ],
          unitPrice: [
            { required: true, message: '预算单价(元)不能为空', trigger: 'blur' }
          ],
          sumPrice: [
            { required: true, message: '小计不能为空', trigger: 'blur' }
          ],
          startDate: [
            { required: true, message: '开始交货期不能为空', trigger: 'blur' }
          ],
          endDate: [
            { required: true, message: '结束交货期不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '交货地点不能为空', trigger: 'blur' }
          ],
          authorId: [
            { required: true, message: '编制人序号不能为空', trigger: 'blur' }
          ],
          author: [
            { required: true, message: '编制人不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '联系电话不能为空', trigger: 'blur' }
          ],
          fax: [
            { required: true, message: '联系传真不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '电子邮件不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/orders/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.orderNum = data.orders.orderNum
                this.dataForm.orderSource = data.orders.orderSource
                this.dataForm.materialNum = data.orders.materialNum
                this.dataForm.materialName = data.orders.materialName
                this.dataForm.amount = data.orders.amount
                this.dataForm.measureUnit = data.orders.measureUnit
                this.dataForm.unitPrice = data.orders.unitPrice
                this.dataForm.sumPrice = data.orders.sumPrice
                this.dataForm.startDate = data.orders.startDate
                this.dataForm.endDate = data.orders.endDate
                this.dataForm.address = data.orders.address
                this.dataForm.authorId = data.orders.authorId
                this.dataForm.author = data.orders.author
                this.dataForm.phone = data.orders.phone
                this.dataForm.fax = data.orders.fax
                this.dataForm.email = data.orders.email
                this.dataForm.remark = data.orders.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/orders/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderNum': this.dataForm.orderNum,
                'orderSource': this.dataForm.orderSource,
                'materialCode': this.dataForm.materialNum,
                'materialName': this.dataForm.materialName,
                'amount': this.dataForm.amount,
                'measureUnit': this.dataForm.measureUnit,
                'unitPrice': this.dataForm.unitPrice,
                'sumPrice': this.dataForm.sumPrice,
                'startDate': this.dataForm.startDate,
                'endDate': this.dataForm.endDate,
                'address': this.dataForm.address,
                'authorId': this.dataForm.authorId,
                'author': this.dataForm.author,
                'phone': this.dataForm.phone,
                'fax': this.dataForm.fax,
                'email': this.dataForm.email,
                'remark': this.dataForm.remark
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
